package cn.itsource.controller.blog;


import cn.itsource.blog.domain.comment.Comment;
import cn.itsource.service.blog.ICommentService;
import cn.itsource.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
@Api(value = "评论接口类",description = "处理评论的所有请求")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    //添加和修改：Put http://localhost:8080/comment {}前端传递json数据 - 后端用@RequestBody
    @PutMapping
    @ApiOperation(value = "添加和修改接口")
    public JsonResult addOrUpdate(@RequestBody Comment comment){
        try {
            if(comment.getId()==null){//添加
                commentService.add(comment);
            }else{
                commentService.update(comment);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/{articleId}")
    public List<Comment> findByArticleId(@PathVariable("articleId") Long articleId){
        return commentService.findByArticleId(articleId);
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") String id) {
        try {
            commentService.delete(id);

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("该评论不存在");
        }
        return null;
    }
}
